const Router = require('@koa/router');
const send = require('koa-send');

const router = new Router();

//--- Root Path -----------

router.get('/', async (ctx, next) => {
    // ctx.router available
    // ctx.body = ' body in root ';
    console.log(' get root. ');
    await send(ctx, '/index.html');
    next();
});

//--- Gold Price ------------
const { PriceWS } = require('../ws/goldPrice');

router.get('/goldprice/ws', async (ctx, next) => {
    if (ctx.ws) {
        const ws = await ctx.ws();
        /* The Code below will trigger Interval fetch continuous  */
        PriceWS(ws);
    }
});

//--- Globla Context------------
const { GloblaContextWS } = require('../ws/globalContext');

router.get('/globalContext/ws', async (ctx, next) => {
    if (ctx.ws) {
        const ws = await ctx.ws();
        GloblaContextWS(ws);
    }
});

//------- Mouse Action --------------
const { MouseRouter } = require('./mouse');

MouseRouter(router);

//------- Trigger Action --------------
const { TriggerRouter } = require('./trigger');
TriggerRouter(router);

//--------- Static Files ------------
router.all(/((\.js)|(\.jsx))|(\.vue)$/i, async (ctx, next) => {
    console.log(' *.js ', ctx.path, Date.now());
    ctx.set('Content-Type', 'application/x-javascript');
    await send(ctx, ctx.path);
    next();
});

router.all(/((\.html)|(\.htm))$/i, async (ctx, next) => {
    await send(ctx, ctx.path);
    next();
});

router.all(/\.css$/i, async (ctx, next) => {
    ctx.set('Content-Type', 'text/css')
    await send(ctx, ctx.path);
    next();
});

module.exports.router = router;